package com.hdcx.Minfresh.fragment;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hdcx.Minfresh.R;
import com.hdcx.Minfresh.activity.GoodListActivity;
import com.hdcx.Minfresh.activity.MainActivity;
import com.hdcx.Minfresh.adapter.ClassifyLeftAdapter;
import com.hdcx.Minfresh.adapter.ClassifyRightAdapter;
import com.hdcx.Minfresh.bean.GoodSortWrapper;
import com.hdcx.Minfresh.url.MyUrl;
import com.hdcx.Minfresh.util.AppUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/11/9.
 */

public class ClassifyFragment extends BaseFragment {
    private ClassifyLeftAdapter leftadapter;
    private ClassifyRightAdapter rightadapter;
    private RecyclerView leftrecycler, rightrecycler;
    private RelativeLayout nothing;
    EditText ed_search;
    Button bt_search;


    @Override
    public int getLayout() {
        return R.layout.fragment_classify;
    }

    @Override
    public void initView(View view) {
        leftrecycler = (RecyclerView) view.findViewById(R.id.recycler);
        rightrecycler = (RecyclerView) view.findViewById(R.id.recycler2);
        nothing = (RelativeLayout) view.findViewById(R.id.layout_nothing);
        ed_search = (EditText) view.findViewById(R.id.ed_search);
        bt_search = (Button) view.findViewById(R.id.bt_search);
        bt_search.setOnClickListener(this);
    }

    @Override
    public void initData() {
        leftadapter = new ClassifyLeftAdapter(getActivity(), this);
        rightadapter = new ClassifyRightAdapter(getActivity(), rightrecycler);
        leftrecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        leftrecycler.setAdapter(leftadapter);
        rightrecycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        rightrecycler.setAdapter(rightadapter);
        post_main();
    }

    /* list 分类列表
     -name 分类名称
     -images 分类图片
     -son  子分类
 */
    public void post_main() {
        AppUtil.showLoading(getActivity());
        OkHttpUtils.postString()
                .url(MyUrl.GOODSORT)
                .content("")
                .build()
                .execute(new Callback<GoodSortWrapper>() {
                    @Override
                    public GoodSortWrapper parseNetworkResponse(Response response) throws IOException {
                        String json = response.body().string();
                        return new Gson().fromJson(json, GoodSortWrapper.class);
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        AppUtil.closeLoading();
                    }

                    @Override
                    public void onResponse(GoodSortWrapper response) {
                        AppUtil.closeLoading();
                        if (response.getStatus() == 1) {
                            leftadapter.setData(response);
                            rightadapter.setData(response);
                            //if(response.getList())
                            MainActivity activity = (MainActivity) mActivity;
                            if (activity.isFromMainFragmentToClassify) {
                                leftadapter.setmPosition(activity.id);
                                activity.isFromMainFragmentToClassify = false;
                            }
                        }
                    }
                });
    }


    @Override
    public void onMyItemClick(Object data) {
        super.onMyItemClick(data);
        rightadapter.change((int) data);
        Log.e("ClassifyFragment", "ClassifyFragment--onMyItemClick--分类"+(int)data);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (leftadapter != null) {
                MainActivity activity = (MainActivity) mActivity;
                if (activity.isFromMainFragmentToClassify) {
                    leftadapter.setmPosition(activity.id);
                    activity.isFromMainFragmentToClassify = false;
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_search:
                String search = ed_search.getText().toString().trim();
                if (search.equals("")) {
                    Toast.makeText(mActivity, "关键字不能为空！", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent = new Intent(mActivity, GoodListActivity.class);
                    intent.putExtra("kw", search);
                    mActivity.startActivity(intent);
                }
        }
    }
}
